package com.weight.system.menuauth.web;

import com.weight.system.menuauth.PO.MenuAuthInfo;
import com.weight.system.menuauth.VO.MenuAuthInfoVO;
import com.weight.system.menuauth.service.MenuAuthInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 菜单权限表
 */
@RestController
@Slf4j
@RequestMapping("/menuauth/menuAuthInfo")
public class MenuAuthInfoController {

	@Autowired
	private MenuAuthInfoService menuAuthInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询菜单权限表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForMenuAuthInfoList",method=RequestMethod.GET)
	public ModelAndView queryForMenuAuthInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/menuauth/queryForMenuAuthInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询菜单权限表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForMenuAuthInfoListDG",method=RequestMethod.GET)
	public String queryForMenuAuthInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String menuId = request.getParameter("menuId");
			String operId = request.getParameter("operId");
			String authStatus = request.getParameter("authStatus");
			String authAdd = request.getParameter("authAdd");
			String authDel = request.getParameter("authDel");
			String authEdit = request.getParameter("authEdit");

			Map<Object, Object> map =new HashMap<>();
			map.put("menuId", menuId);
			map.put("operId", operId);
			map.put("authStatus", authStatus);
			map.put("authAdd", authAdd);
			map.put("authDel", authDel);
			map.put("authEdit", authEdit);

			
			//2、查询菜单权限表列表
			List<MenuAuthInfo> menuAuthInfoList = menuAuthInfoService.findMenuAuthInfoByPage(page,rows,map);
			log.info("menuAuthInfoList={}",menuAuthInfoList);
			List<MenuAuthInfoVO> menuAuthInfoVOList = menuAuthInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<MenuAuthInfoVO> result = new PageResult<MenuAuthInfoVO>(menuAuthInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param menuAuthInfoVO
	 * @param menuAuthInfo
	 * @return
	 */
	public void changeVOToPO(MenuAuthInfoVO menuAuthInfoVO,MenuAuthInfo menuAuthInfo){
			BeanUtils.copyProperties(menuAuthInfoVO,menuAuthInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param menuAuthInfoVO
	 * @param menuAuthInfo
	 * @return
	 */
	public void changePoToVO(MenuAuthInfoVO menuAuthInfoVO,MenuAuthInfo menuAuthInfo){
			BeanUtils.copyProperties(menuAuthInfo,menuAuthInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param menuAuthInfo
	 * @return
	 */
	public MenuAuthInfoVO changePoToVO(MenuAuthInfo menuAuthInfo){
			MenuAuthInfoVO menuAuthInfoVO=new MenuAuthInfoVO();
			BeanUtils.copyProperties(menuAuthInfo,menuAuthInfoVO);
			

			return menuAuthInfoVO;
	}

	/**
	 * 3.查询菜单权限表--详情页
	 * @param menuAuthId
	 * @return
	 */
	@RequestMapping("/queryForMenuAuthInfoDetail")
	public ModelAndView queryForMenuAuthInfoDetail(@RequestParam(value="menuAuthId",required=false) String menuAuthId){
			//1.校验id
			CommonUtils.validateExistID(menuAuthId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("menuAuthId", menuAuthId);
			log.info("查询菜单权限表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			MenuAuthInfo menuAuthInfo = menuAuthInfoService.findByMenuAuthInfoId(map);
			
			MenuAuthInfoVO menuAuthInfoVO = changePoToVO(menuAuthInfo);
			//3、返回前台
			log.info("查询菜单权限表查询成功：menuAuthInfo={}",menuAuthInfo);
			modelAndView.setViewName("/system/menuauth/queryForMenuAuthInfoDetail");
			modelAndView.addObject("menuAuthInfoVO",menuAuthInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改菜单权限表操作
	 * @param menuAuthId
	 * @return
	 */
	@RequestMapping(value="/addMenuAuthInfo")
	public ModelAndView addMenuAuthInfo(@RequestParam(value="menuAuthId",required=false) String menuAuthId){
			//1、创建返回对象
			MenuAuthInfoVO menuAuthInfoVO = new MenuAuthInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(menuAuthId)) {
				Map<String, String> map=new HashMap<>();
				map.put("menuAuthId", menuAuthId);
				log.info("修改操作，查询ID：menuAuthId={}",menuAuthId);
				MenuAuthInfo menuAuthInfo = menuAuthInfoService.findByMenuAuthInfoId(map);
				this.changePoToVO(menuAuthInfoVO,menuAuthInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/menuauth/addMenuAuthInfo");
			modelAndView.addObject("menuAuthInfoVO",menuAuthInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改菜单权限表操作
	 * @param menuAuthInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveMenuAuthInfo")
	public JsonResultUtil saveMenuAuthInfo(@Valid MenuAuthInfoVO menuAuthInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,menuAuthInfoVO);
			
			//2.获取id
			String menuAuthId=menuAuthInfoVO.getMenuAuthId();
			MenuAuthInfo menuAuthInfo=new MenuAuthInfo();
			BeanUtils.copyProperties(menuAuthInfoVO,menuAuthInfo);
			this.changeVOToPO(menuAuthInfoVO,menuAuthInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(menuAuthId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("menuAuthId", menuAuthId);
				log.info("修改操作，查询ID：menuAuthId={}",menuAuthId);
				//查询是否存在
				menuAuthInfoService.findByMenuAuthInfoId(map);
				try{
					menuAuthInfo.setUpdateBy("000");
					menuAuthInfoService.updateMenuAuthInfo(menuAuthInfo);
					log.info("更新操作成功：menuAuthInfo={}",menuAuthInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					menuAuthInfo.setMenuAuthId(CommonUtils.getUUId());
					menuAuthInfo.setCreateBy("000");
					menuAuthInfo.setUpdateBy("000");
					menuAuthInfoService.insertMenuAuthInfo(menuAuthInfo);
					log.info("添加操作成功：menuAuthInfo={}",menuAuthInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除菜单权限表操作
	 * @param menuAuthId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByMenuAuthInfoId")
	public JsonResultUtil deleteByMenuAuthInfoId(@RequestParam(value="menuAuthId",required=false) String menuAuthId){
			//1.校验id
			CommonUtils.validateExistID(menuAuthId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("menuAuthId", menuAuthId);
			menuAuthInfoService.findByMenuAuthInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除菜单权限表入参，map={}",map);
			
			try{
				menuAuthInfoService.upDelByMenuAuthInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除菜单权限表操作---批量删除
	 * @param menuAuthIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByMenuAuthInfoIdBatch")
	public JsonResultUtil deleteByMenuAuthInfoIdBatch(@RequestParam(value="menuAuthIdList",required=false) List menuAuthIdList){
			//1.校验id
			CommonUtils.validateExistIDList(menuAuthIdList);
			
			//2.校验数据库
			menuAuthInfoService.findByMenuAuthInfoIdList(menuAuthIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("menuAuthIdList", menuAuthIdList);
			map.put("updateBy","000");
			log.info("批量删除菜单权限表操作入参，map={}",map);
			
			try{
				menuAuthInfoService.upDelByMenuAuthInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}